<?php

namespace common\models;

use Yii;
use common\models\Doc;
use common\models\DocSearch;

use common\models\Word;
use common\models\Creditdetail;
/**
 * This is the model class for table "user".
 *
 * @property int $uid
 * @property string $username
 * @property string $auth_key  
 * @property string $password_hash
 * @property string $email
 * @property int $credit
 * @property int $groupid
 * @property int $createnum
 * @property int $editnum
 * @property string $password_reset_token
 */
class User extends \yii\db\ActiveRecord implements IdentityInterface
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'user';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['auth_key'], 'required'],
            [['credit', 'groupid', 'createnum', 'editnum'], 'integer'],
            [['username', 'email'], 'string', 'max' => 128],
            [['password_hash', 'password_reset_token'], 'string', 'max' => 255],

        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'uid' => '用户ID',
            'username' => '用户名',
            'auth_key' => 'Auth Key',			
            'password_hash' => 'Password Hash',
            'email' => 'Email',
            'credit' => '积分',
            'groupid' => '分组',
            'createnum' => '创建词条数目',
            'editnum' => '编辑词条数目',
            'password_reset_token' => 'Password Reset Token',			
        ];
    }
	    public function getBeginning($length=50)
    {
    	$tmpStr = strip_tags($this->content);
    	$tmpLen = mb_strlen($tmpStr);
    	 
    	$tmpStr = mb_substr($tmpStr,0,$length,'utf-8');
    	return $tmpStr.($tmpLen>$length?'...':'');
    }

	   public function getUrl()
    {
    	return Yii::$app->urlManager->createUrl(
    			['doc/detail','did'=>$this->did,'title'=>$this->title]);
    }
	    public function  getKeywordLinks()
    {
    	$links=array();
    	foreach(Word::string2array($this->keywords) as $word)
    	{
    		$links[]=Html::a(Html::encode($word),array('doc/index','DocSearch[keywords]'=>$word));
    	}
    	return $links;
    }
	    public static function findRecentDocs($limit=10)
    {
    	return Doc::find()->where(['locked'=>0])->orderBy('time DESC')
    	->limit($limit)->all();
    } 
	public static function findHotDocs($limit=10)
    {
    	return Doc::find()->where(['locked'=>0])
    	->limit($limit)->all();
    } 	
}
